/*
 * Copyright (c) [2019-2020] [NorthLan](lan6995@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.lan.iti.cloud.jackson.dynamicfilter.support;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonObjectFormatVisitor;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.ser.PropertyFilter;
import com.fasterxml.jackson.databind.ser.PropertyWriter;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;

/**
 * {@link PropertyFilter} 的代理器
 * 对 {@link com.fasterxml.jackson.databind.ObjectMapper} 进行转化配置
 *
 * @author NorthLan
 * @date 2020-03-05
 * @url https://noahlan.com
 */
public class DynamicFilterProvider extends SimpleFilterProvider implements PropertyFilter {
    private static final long serialVersionUID = -2360020187654999458L;
    public static final String FILTER_ID = "DynamicFilterProvider$FILTER";

    private final PropertyFilter delegate;

    public DynamicFilterProvider(PropertyFilter filter) {
        if (filter != null) {
            this.delegate = filter;
        } else {
            this.delegate = SimpleBeanPropertyFilter.serializeAll();
        }
        addFilter(FILTER_ID, this);
    }

    @Override
    public void serializeAsField(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider, PropertyWriter propertyWriter) throws Exception {
        delegate.serializeAsField(o, jsonGenerator, serializerProvider, propertyWriter);
    }

    @Override
    public void serializeAsElement(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider, PropertyWriter propertyWriter) throws Exception {
        delegate.serializeAsElement(o, jsonGenerator, serializerProvider, propertyWriter);
    }

    @SuppressWarnings("deprecation")
    @Override
    public void depositSchemaProperty(PropertyWriter propertyWriter, ObjectNode objectNode, SerializerProvider serializerProvider) throws JsonMappingException {
        delegate.depositSchemaProperty(propertyWriter, objectNode, serializerProvider);
    }

    @Override
    public void depositSchemaProperty(PropertyWriter propertyWriter, JsonObjectFormatVisitor jsonObjectFormatVisitor, SerializerProvider serializerProvider) throws JsonMappingException {
        delegate.depositSchemaProperty(propertyWriter, jsonObjectFormatVisitor, serializerProvider);
    }
}
